home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / ResEdit / ResEdit 2.0b2 / Examples / PExamples / Source / XXXX.Edit.p < prev   
Encoding:
Text File  |  1990-04-02  |  9.8 KB  |  327 lines  |  [TEXT/MPS ]

  1. {
  2. File ResXXXXEd.p
  3.  
  4. Copyright Apple Computer, Inc. 1985-1990
  5. All rights reserved.
  6. }
  7.  
  8. UNIT ResXXXXed;
  9. {XXXX Editor for ResEdit}
  10.  
  11. INTERFACE
  12.  
  13.     USES    Memtypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
  14.                 ResEd;
  15.  
  16.         {$R-} {Range checking off }
  17.  
  18.     TYPE
  19.         rXXXXPtr = ^rXXXXRec;
  20.         rXXXXHandle = ^rXXXXPtr;
  21.         rXXXXRec = RECORD
  22.                                  father: ParentHandle;        { Back ptr to dad }
  23.                                  name: str255;                         { The name of this editor }
  24.                                  windPtr: WindowPtr;            { This view's window }
  25.                                  rebuild: BOOLEAN;                { Set TRUE if things have changed }
  26.                                  hXXXX: Handle;                        { The resource we are working on }
  27.                                  resWasntLoaded: BOOLEAN;    { True if the resource wasn't loaded before we started. }
  28.                              END; {rXXXXRec}
  29.  
  30.     PROCEDURE EditBirth(thing: Handle; Dad: ParentHandle);
  31.  
  32.     PROCEDURE PickBirth(t: ResType; Dad: ParentHandle);
  33.  
  34.     PROCEDURE DoEvent(VAR Evt: EventRecord; myXXXX: rXXXXHandle);
  35.  
  36.     PROCEDURE DoInfoUpdate(oldID, newID: INTEGER; myXXXX: rXXXXHandle);
  37.  
  38.     FUNCTION IsThisYours (thing: Handle; myXXXX:rXXXXHandle): BOOLEAN;
  39.  
  40.     PROCEDURE DoMenu(Menu, Item: INTEGER; myXXXX: rXXXXHandle);
  41.  
  42. IMPLEMENTATION
  43.  
  44.     CONST
  45.         windowWidth = 300;
  46.         windowHeight = 100;
  47.  
  48.         deleteChr = CHR(8);
  49.         
  50.         sizeOfMyResource = 10;
  51.         
  52.         {- - -    -  - -    -  -    - -  -    - - -  - -    - - -  -    - -  -    -}
  53.  
  54.     { Fix up the window name and title for our window. }
  55.     PROCEDURE GetNameAndTitle(VAR windowTitle, windowName: STR255; thing: Handle);
  56.  
  57.         BEGIN
  58.         windowTitle := '';
  59.         SetETitle(thing, windowTitle);
  60.         windowName := Concat('XXXX', windowTitle);
  61.         windowTitle := Concat('XXXX', windowTitle);
  62.         END;
  63.  
  64.  { **************************************************************************************** }
  65.  
  66.     PROCEDURE EditBirth(thing:Handle; dad:ParentHandle);
  67.  
  68.         VAR
  69.             myXXXX: rXXXXHandle;
  70.             myWindow: WindowPtr;
  71.             windowTitle, newName: STR255;
  72.  
  73.         BEGIN {EditBirth}
  74.         { Prepare window title and request creation of a new window }
  75.         GetNameAndTitle(windowTitle, newName, thing);
  76.         myWindow := EditorWindSetup(noDialog, FALSE, windowWidth, windowHeight, windowTitle, newName, TRUE, ResEdId, dad);
  77.         
  78.         { If we got a new window, then start up the editor }
  79.         IF myWindow <> NIL THEN
  80.             BEGIN
  81.             IF (GetHandleSize(thing)) = 0 THEN { This was called via a NEW, so make a new resource }
  82.                 FixHand(sizeOfMyResource, thing);
  83.  
  84.             { Get memory for and handle to our instance record }
  85.             myXXXX := rXXXXHandle(NewHandle(SIZEOF(rXXXXRec)));
  86.             BubbleUp(Handle(myXXXX));
  87.             HLock(Handle(myXXXX));
  88.  
  89.             WITH myXXXX^^ DO
  90.                 BEGIN
  91.                 { Put information about this incarnation of the editor and the window it is }
  92.                 { serving into our record.(always passed around in the handle myXXXX).  }
  93.                 
  94.                 windPtr := myWindow;
  95.                 father := dad;
  96.                 name := newName;
  97.                 hXXXX := thing;
  98.                 resWasntLoaded := NOT WasItLoaded;
  99.                 
  100.                 { Let the main program know who is to manage this window by giving it our     }
  101.                 { instance record handle.        }
  102.                 WindowPeek(myWindow)^.refCon := ORD(myXXXX);
  103.                 END; {WITH}
  104.                 
  105.             { Set up any menus,views, etc. for this window here. }
  106.             
  107.             HUnlock(Handle(myXXXX));
  108.             END; { IF myWind <> NIL }
  109.         END; { EditBirth }
  110.     {- -    -  - - -    - -  - - -    -  - -    -  -    - -  -    - - -  - -}
  111.  
  112.     { Not used for editors. }
  113.     PROCEDURE PickBirth(t:ResType;Dad:ParentHandle);
  114.  
  115.         BEGIN { PickBirth }
  116.         END;     { PickBirth }
  117.     {- -    -  - - -    - -  - - -    -  - -    -  -    - -  -    - - -  - -}
  118.  
  119.     PROCEDURE DoEvent(VAR Evt:EventRecord; myXXXX:rXXXXHandle);
  120.  
  121.         VAR
  122.             MousePoint: Point;
  123.             act: BOOLEAN;
  124.  
  125.         BEGIN {DoEvent}
  126.         BubbleUp(Handle(myXXXX));         { Move our item up in memory }
  127.         HLock(Handle(myXXXX));                 { Lock it down }
  128.         WITH myXXXX^^ DO
  129.             BEGIN
  130.             { Handle event passed to us by main program.  Just like a 'real' event loop, except…
  131.                  there is no loop and we don't have to handle as much because the main program
  132.                  will do all the stuff that doesn't apply to us. }
  133.                 
  134.             MousePoint := Evt.where;        { Point at which the event occured }
  135.             SetPort(windPtr);                        { Set the port to our window }
  136.             GlobalToLocal(MousePoint);    { Convert event location to local coords }
  137.             
  138.             CASE Evt.what OF
  139.                 mouseDown:
  140.                     BEGIN
  141.                                                 { Do any special mouse down processing here. }
  142.                     END; { mouseDown }
  143.                 activateEvt:
  144.                     BEGIN
  145.                     AbleMenu(fileMenu, fileAll);
  146.                     AbleMenu(rsrcMenu, rsrcEditor);
  147.                     act := ODD(Evt.modifiers);
  148.                     IF act THEN
  149.                         BEGIN
  150.                                                 { Do any activate processing here (such as inserting a menu). }
  151.                         END
  152.                     ELSE
  153.                         BEGIN
  154.                                                 { Do any deactivate processing here (such as deleting a menu). }
  155.                         END;
  156.                     END {activateEvt} ;
  157.                 updateEvt:
  158.                     BEGIN
  159.                                                 { Do the appropriate update processing here.  Remember that 
  160.                                                     BeginUpdate has already been called. }
  161.                     PaintRect(windPtr^.portrect);
  162.                     END; {updateEvt}
  163.                 keyDown:
  164.                     BEGIN
  165.                                                 { Do any key processing here. }
  166.                     
  167.                     { Convert the delete character into a clear command. }
  168.                     IF (CHR(band(evt.message, charCodeMask)) = deleteChr) THEN
  169.                         DoMenu (editMenu, clearItem, myXXXX)
  170.                     END; {keyDown}
  171.                 
  172.                 nullEvent:
  173.                     BEGIN
  174.                                             { Do any null event processing here (such as blinking a cursor).    }
  175.                     END;
  176.                 END;    { CASE evt.what }
  177.             END;         { WITH myXXXX^^ }
  178.             
  179.         HUnlock(Handle(myXXXX));
  180.         END;             { DoEvent }
  181.     {- -    -  - - -    - -  - - -    -  - -    -  -    - -  -    - - -  - -}
  182.  
  183.     PROCEDURE DoInfoUpdate(oldID,newID:INTEGER;myXXXX:rXXXXHandle);
  184.  
  185.         VAR
  186.             windowTitle, windowName: STR255;
  187.  
  188.         BEGIN { DoInfoUpdate }
  189.         HLock(Handle(myXXXX));                 { Lock it down }
  190.         WITH myXXXX^^ DO
  191.             BEGIN 
  192.             { Since our ID has changed, we need to change our window title }
  193.             GetNameAndTitle (windowTitle, windowName, Handle(hXXXX));
  194.             GetWindowTitle (windowTitle, windowName, TRUE, father);
  195.             name := windowName;                                { Save the new name in my data structure. }
  196.             SetWTitle(windPtr, windowtitle);    { Set the new window title. }
  197.     
  198.             { Now, let our father object know that our ID has been changed }
  199.             father^^.rebuild := TRUE;                    { Rebuild the picker list. }
  200.             CallInfoUpdate(oldID, newID, father^^.wind^.refCon, father^^.wind^.windowKind);
  201.             END; { WITH myXXXX^^ }
  202.         HUnlock(Handle(myXXXX));
  203.         END; { DoInfoUpdate }
  204.  
  205.     {- -    -  - - -    - -  - - -    -  - -    -  -    - -  -    - - -  - -}
  206.         
  207.     FUNCTION IsThisYours (thing: Handle; myXXXX:rXXXXHandle): BOOLEAN;
  208.     
  209.         BEGIN
  210.         IsThisYours := (Handle(myXXXX^^.hXXXX) = thing);
  211.         END;
  212.  
  213.     {- -    -  - - -    - -  - - -    -  - -    -  -    - -  -    - - -  - -}
  214.  
  215.     PROCEDURE DoMenu(Menu, Item:INTEGER; myXXXX:rXXXXHandle);
  216.  
  217.         VAR
  218.             saveRefNum: INTEGER;
  219.             hTemp: Handle;
  220.  
  221.         { Close down the window and get rid of any memory that has been allocated. }
  222.         FUNCTION DoClose: BOOLEAN;
  223.  
  224.             BEGIN
  225.             PassMenu(fileMenu, closeItem, ParentHandle(myXXXX));
  226.             IF WasAborted THEN
  227.                 DoClose := FALSE
  228.             ELSE
  229.                 BEGIN
  230.                 WITH myXXXX^^ DO
  231.                     BEGIN
  232.                     CloseWindow(windPtr);
  233.                     WindReturn(windPtr);                { Mark the window record as being available }
  234.                     SetTheCursor (arrowCursor); { Make sure the cursor is the arrow cursor }
  235.                     
  236.                     { Delete any menus that we added and the redraw menu bar     }
  237.                     { Be sure to dispose of any handles you are done with    }
  238.                     
  239.                     { Release the resource if we were launched from a picker }
  240.                     IF resWasntLoaded & (father^^.name[1] <> editorNameChr) THEN
  241.                         ReleaseResource (Handle(hXXXX));  { Let it be free (if it is not changed)! }
  242.                     END; { WITH myXXXX^^ }
  243.                 DisposHandle(Handle(myXXXX));
  244.                 DoClose := TRUE;
  245.                 END;
  246.             END; { DoClose }
  247.  
  248.         BEGIN {DoMenu}
  249.         BubbleUp(Handle(myXXXX));
  250.         HLock(Handle(myXXXX));
  251.         WITH myXXXX^^ DO
  252.             BEGIN
  253.             SetPort(windPtr); { Set the port to our window }
  254.             
  255.             { Again, we handle the menu stuff just as we would in a 'real' application
  256.                 except that we only have to handle those items that apply to our editor. }
  257.             CASE Menu OF
  258.                 fileMenu:
  259.                     CASE Item OF
  260.                         CloseItem:
  261.                             BEGIN
  262.                             IF DoClose THEN             {    Close our window }
  263.                                 EXIT(DoMenu);             {    Return immediately since our resource is gone!    }
  264.                             END; { CloseItem }
  265.                             
  266.                         saveItem:                { Pass the save on to other windows. }
  267.                             PassMenu(fileMenu, saveItem, ParentHandle(myXXXX));
  268.                             
  269.                         printItem:
  270.                             PrintWindow (NIL);
  271.                         END; { Case }
  272.                         
  273.                 rsrcMenu:
  274.                     CASE item OF
  275.                         rsrcRevertItem:
  276.                             BEGIN
  277.                             IF NeedToRevert (windPtr, Handle(hXXXX)) THEN
  278.                                 BEGIN
  279.                                 { The area under the window will need to be updated }
  280.                                 InvalRect(windPtr^.portrect);
  281.                                 
  282.                                 { We will need to restore the current resource file reference number when we are done here.     }
  283.                                 saveRefNum := CurrentRes;
  284.                                 { We are going to be using the resource file we came from. }
  285.                                 UseResFile(HomeResFile(Handle(hXXXX)));
  286.                                 
  287.                                 { Read in the old copy from disk (see documentation for revertResource).  Clear it out 
  288.                                     unless this  was a newly created resource, in which case don't. }
  289.                                 IF NOT RevertThisResource(ParentHandle(myXXXX), Handle(hXXXX)) THEN
  290.                                     BEGIN        { The resource was newly added so we need to remove it. }
  291.                                     { Save the handle so that we can dispose it later. }
  292.                                     hTemp := Handle (hXXXX);
  293.                                     
  294.                                     { Make sure that the picker list is rebuilt to remove this item. }
  295.                                     myXXXX^^.father^^.rebuild := TRUE;
  296.                                     
  297.                                     IF DoClose THEN                                { Close the window. }
  298.                                         BEGIN
  299.                                         RemoveResource (hTemp);    { Dispose the resource itself. }
  300.                                         EXIT(DoMenu);
  301.                                         END;
  302.                                     END; {IF NOT RevertResource…}
  303.                                 
  304.                                 UseResFile(saveRefNum);    { Go back to using the old resource file. }
  305.                                 END;
  306.                             END; { RevertItem }
  307.                             
  308.                         rsrcGetInfoItem:
  309.                             BEGIN
  310.                             { Put up the GetInfo window. }
  311.                             ShowInfo(Handle(hXXXX), ParentHandle(myXXXX));
  312.                             END; { GetInfoItem }
  313.                     END; {rsrcmenu: CASE Item OF}
  314.                 EditMenu:
  315.                     CASE Item OF
  316.                         { Implement the edit menu here. }
  317.                         CutItem: ;
  318.                         CopyItem: ;
  319.                         PasteItem: ;
  320.                         ClearItem: ;
  321.                     END; { EditMenu }
  322.                 END; { CASE Menu OF }
  323.             END; { WITH myXXXX^^}
  324.         HUnlock(Handle(myXXXX));
  325.         END; {DoMenu}
  326. END.
  327.